package datahandling;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.*;
import java.util.regex.*;


public class Analyzer{
	//Analyzer for txt
	//
	public static String[] getFileInfo(String path){
		String[] aStr = new String[2];
		//正则匹配到文件名中的标签位
		Pattern pattern = Pattern.compile(".*At(\\d+).*");
		Matcher matcher = pattern.matcher(path);
		while(matcher.find()){
			aStr[0] = matcher.group(1); 
		}
		//匹配得到文件名
		Pattern pattern1 = Pattern.compile(".*/(.*)");
		Matcher matcher1 = pattern1.matcher(path);
		while(matcher1.find()){
			aStr[1] = matcher1.group(1); 
		}
		return aStr;
	}
	
	public static List<Integer> readLines(String path,String fileName,int label_pos){
        List<Integer> rowList = new ArrayList<Integer>();
        try{  
            FileInputStream in = new FileInputStream(path);  
            InputStreamReader inReader = new InputStreamReader(in, "UTF-8");  
            BufferedReader bufReader = new BufferedReader(inReader);  
            String line = null;  
            int i = 0;
            System.out.println("Reading the "+fileName+":");
            while((line = bufReader.readLine()) != null ){  
            	//忽略空行
				String pattern = "\\s+";
				boolean isBlank = Pattern.matches(pattern,line);
				if(line.equals("") || isBlank){
            		System.out.println("\t there has none-line!");
            		continue;
            	}
				// 实例化Line对象
                Line temp = new Line(line,label_pos,i);
                //列表记录每一行列数
                rowList.add(temp.getRowNum());
                i++;
            }  
            bufReader.close();  
            inReader.close();  
            in.close();  
        }
        catch (Exception e) {  
            e.printStackTrace();  
            System.out.println("读取出错！");  
        }  
        return rowList;
	}
	
	public static int getDimension(List<Integer> rowList){
        HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
        for(int i = 0; i < rowList.size(); i++){
            if(map.containsKey(rowList.get(i)))        {
                int formerValue = map.get(rowList.get(i));
                map.put(rowList.get(i), formerValue + 1);    // 该数字出现的次数加1
            }
            else{
                map.put(rowList.get(i), 1);    // 该数字第一次出现
            }
        }
        Collection<Integer> count = map.values();
        // 找出map的value中最大值，也就是数组中出现最多的数字所出现的次数
        int maxCount = Collections.max(count);
        int maxNumber = 0;
        for(Map.Entry<Integer, Integer> entry : map.entrySet()){
            //得到value为maxCount的key，也就是数组中出现次数最多的数字
            if(entry.getValue() == maxCount){
                maxNumber = entry.getKey();
            }
        }
        return maxNumber;
	}
	
	
	//Analyzer for lines
	public static void checkLineError(String data,int lineNum){
		String[] single = data.split("\\t");
		if(single[0].length() == data.length()){
			single = data.split(" ");
			if(single[0].length() == data.length())
				single = data.split(",");
		}
		for(String i : single){
	        boolean isInt = Pattern.compile("^-?[1-9]\\d*$").matcher(i).find();
	        boolean isDouble = Pattern.compile("^-?([1-9]\\d*\\.\\d*|0\\.\\d*[1-9]\\d*|0?\\.0+|0)$").matcher(i).find();
	        if(!(isInt || isDouble)){
	        	System.out.println("\t the "+lineNum+" line has error char: "+ i);
	        }
		}
	}
	public static String[] getLineLabel(String data,int label_pos){

		//分隔每行，得到类标签
		String[] aStr = new String[2];
		String[] single = data.split("\\t");
		if(single[0].length() == data.length()){
			single = data.split(" ");
			if(single[0].length() == data.length())
				single = data.split(",");
		}
		aStr[0] = single[label_pos];
		aStr[1] = String.valueOf(single.length);
		//返回列标签及行列数
		return aStr;
	}
}
